//
//  GETnetworkInfo.m
//  ARP Alert
//
//  Created by James Richardson on 7/28/11.
//  Copyright 2011 Home. All rights reserved.
//

#import "GETnetworkInfo.h"
#include "NetworkInfo.h"
#define MAXADDRS	32

@implementation GETnetworkInfo
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

-(void)populateARP:(NSMutableArray*)ARParr{
    
    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init];
    
    int addr = 0;
	int runs = 0;
    BOOL switched = 0;
	
    InitAddresses();
	//GetIPAddresses();
	//GetHWAddresses();
	runs = dump(addr);	
    
    if(runs == 0 && hw_host[0] != NULL && ip_host[0] != NULL && host_host[0] != NULL){
        
		NSMutableDictionary *ARPinfo = [[[NSMutableDictionary alloc] init] autorelease];
        
        [ARPinfo setObject:[NSString stringWithFormat:@"%s", host_host[0]] forKey:@"Host"];
        [ARPinfo setObject:[NSString stringWithFormat:@"%s", hw_host[0]] forKey:@"MAC"];
        [ARPinfo setObject:[NSString stringWithFormat:@"%s", ip_host[0]] forKey:@"IP"];
        [ARPinfo setObject:@"0" forKey:@"Trust"];
        
		[ARParr addObject:ARPinfo];
        
        switched = 1;
        
        FreeAddresses_arp_noNet();
        
	}
    else
    {
        for(int i=0 ; i<runs; i++){
            NSMutableDictionary *ARPinfo = [[[NSMutableDictionary alloc] init] autorelease];
        
            //stringWithFormat:@"%s", hw_host[i]
            [ARPinfo setObject:[NSString stringWithFormat:@"%s",host_host[i]] forKey:@"Host"];
            [ARPinfo setObject:[NSString stringWithFormat:@"%s",hw_host[i]] forKey:@"MAC"];
            [ARPinfo setObject:[NSString stringWithFormat:@"%s",ip_host[i]] forKey:@"IP"];
            [ARPinfo setObject:@"0" forKey:@"Trust"];
    
            [ARParr addObject:ARPinfo];
            
            //free addresses from Networkinfo.c when we are done iterating and storing
            if(runs == i+1)
                FreeAddresses_arp();
        }
    }
    
    
    
    [pool drain];
}

-(void)populateNET:(NSMutableArray*)NETarr{
    
    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init];
	
    InitAddresses();
	GetIPAddresses();
	GetHWAddresses();
    
    for (int i=0; i<MAXADDRS; ++i)
	{
		unsigned long theAddr;
        theAddr = ip_addrs[i];
        
		if (theAddr != 0 && hw_addrs[i] != NULL && if_names[i] != NULL &&  ip_names[i] != NULL && hw_addrs[i] >! MAXADDRS ){
            NSMutableDictionary *NETinfo = [[[NSMutableDictionary alloc] init] autorelease];
        
            [NETinfo setObject:[NSString stringWithFormat:@"%s", if_names[i]] forKey:@"Interface"];
            [NETinfo setObject:[NSString stringWithFormat:@"%s", hw_addrs[i]] forKey:@"MAC"];
            [NETinfo setObject:[NSString stringWithFormat:@"%s", ip_names[i]] forKey:@"IP"];
        
            [NETarr addObject:NETinfo];
        }
    }
    
    FreeAddresses_ip();
    
    [pool release];
}



@end
